package top.hencter.weibo.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import top.hencter.weibo.R;
import top.hencter.weibo.bean.IndexItem;

public class IndexListAdapter extends ArrayAdapter<IndexItem> {
    private int resourceId;
    private Context mContext;
    public IndexListAdapter(Context context, int resource, List<IndexItem> objects) {
        super(context, resource, objects);
        resourceId = resource;
        mContext = context;
    }


    @Override
    public View getView(int position, View convertView,  ViewGroup parent) {
        View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
        IndexItem cardContent = getItem(position);

        // 渲染用户头像
        ImageView avatarView = view.findViewById(R.id.index_avatar);
        Glide.with(getContext()).load(cardContent.getAvatar()).into(avatarView); // 链式调用

        // 渲染用户名
        TextView titleView = view.findViewById(R.id.post_author);
        titleView.setText(cardContent.getName());
        // 渲染博文日期
        TextView dataView = view.findViewById(R.id.post_time);
        dataView.setText(cardContent.getDate());
        // 渲染博文
        TextView contentView = view.findViewById(R.id.content);
        contentView.setText(cardContent.getContent());

        // 渲染内容图片
        GridView gridView = view.findViewById(R.id.index_image_grid_view);
        IndexCardImageAdapter adapter = new IndexCardImageAdapter(getContext(), R.layout.index_gridview_item, cardContent.getImages());
        gridView.setAdapter(adapter);

        // 渲染分享数
        TextView shareCountView = view.findViewById(R.id.share_count);
        shareCountView.setText(cardContent.getShares()+"");

        // 渲染评论数
        TextView commentCountView = view.findViewById(R.id.comment_count);
        commentCountView.setText(cardContent.getComments()+"");

        // 渲染点赞数
        TextView likeCountView = view.findViewById(R.id.like_count);
        likeCountView.setText(cardContent.getLikes()+"");

        // 点赞图标
        ImageView likView = view.findViewById(R.id.like_view);


        likView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取微博 ID
                String id = cardContent.get_id();

                Map<String ,String> map =  new HashMap<>();
                map.put("id", id);
                Gson gson = new Gson();
                String json = gson.toJson(map);
                RequestBody requestBody = RequestBody.create(json, MediaType.parse("application/json; charset-utf-8"));

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("http://129.28.159.96:7001/v1/test/like")
                        .post(requestBody)
                        .build();

                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        Log.i("======", e.toString());
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        ((Activity)getContext()).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                likeCountView.setText(Integer.parseInt(likeCountView.getText().toString()) + 1 + "");
                            }
                        });
                    }
                });
            }
        });

        return view;
    }
}
